<template>
	<view class="inventory-contont">
		<contont-nav-bar></contont-nav-bar>
		<!-- <keyboard-listener @keydown="onKeydown"></keyboard-listener> -->
		<!-- <keyboard-listener @keyup="onKeydown"></keyboard-listener> -->
		<view class="inventory-info">
			<inventory-list :List="inventoryList" @onDetails="onDetails"></inventory-list>
			<view class="inventory-bottom">
				<!-- <inventory-bar @onLocation="onLocation" @onSearchFind="onSearchFind" @onInventory="onInventory"></inventory-bar> -->
				<view class="contont">
					<view class="v1">
						<view class="bottom-l">
							<image @tap="onSearchFind" class="chaxun-img" src="../../static/images/inventory/chaxun.png" mode="widthFix"></image>
							<image @tap="onLocation" class="jiankong1-img" src="../../static/images/inventory/jiankong1.png" mode="widthFix"></image>
						</view>
						<view class="bottom-c">当前库存</view>
						<view class="bottom-r">
							<!-- <image @tap="onInventory" class="jiangkong3-img" src="../../static/images/inventory/jiankong3.png" mode="widthFix"></image> -->
							<view class="back-btn" @tap="onBack"> 
								<image class="back-img" src="/static/images/group/back.png" mode="widthFix"/>
								<text class="back-text">返回</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 详情弹出层 -->
		<uni-popup ref="popup" type="center">
			<!-- <message-contont @onDelete="onDelete" @onClose="onClose"></message-contont> -->
			<view class="popup-push">
				<view class="nav-title">{{mContont.itemName}}</view>
				<view class="nav-contont">
					<view v-show="mContont.bizType==1" class="content-text">上架</view>
					<view v-show="mContont.bizType==2" class="content-text">领用</view>
					<view v-show="mContont.bizType==3" class="content-text">退回</view>
					<view class="content-end">{{mContont.operatorName}}</view>
					<view class="content-date">{{mContont.operTime}}</view>
				</view>
				<view class="nav-bottom">
					<view class="left-tupiao">
					</view>
					<view class="center-btn" @tap="onClose">
						<button class="btn" type="default">关闭</button>
					</view>
					<view class="kong"></view>
				</view>
			</view>
		</uni-popup>
		<!-- 材料位置弹出层 -->
		<uni-popup ref="popup-location" type="center">
			<view class="location-find">
				<view class="layber-on">
					<view class="nav-title">耗材定位</view>
					<image class="nav-guanbi" @tap="$refs['popup-location'].close()" src="../../static/images/inventory/guanbi1.png" mode="widthFix"></image>
				</view>
				<view class="layber-in">
					<view class="layber-name">材料名称:</view>
					<view class="layber-info">
						<input class="layber-input" type="text" v-model="message" />
						<button class="layber-btn" type="default" @tap="onfind(message)">查询</button>
					</view>
				</view>
			</view>
		</uni-popup>
		
		<!-- 查询弹出层 -->
		<uni-popup ref="popup-datafind" type="center">
			<view class="find-data">
				<view class="top">
					<view class="top-l">
						<text class="top-text">查询</text>
					</view>
					<image class="top-r" @tap="Close" src="../../static/guanbihei.png" mode="widthFix"></image>
				</view>
				<view class="nav">
					<view class="nav-text">科室:</view>
					<view class="input-box">
						<input type="text" class="nav-input" v-model="department" />
					</view>
					<view class="nav-text">人员:</view>
					<view class="input-box">
						<input type="text" class="nav-input" v-model="personnel" />
					</view>
					<view class="nav-text">耗材种类:</view>
					<view class="input-box">
						<input type="text" class="nav-input" v-model="types" />
						<!-- <image class="drop-down" src="../../static/zhankai.png" mode="widthFix"></image> -->
					</view>
				</view>
				<view class="time-list">
					<view class="time-text">时间段:</view>
					<view class="time-selector">
						<view class="selector">
							<view class="data-text" @tap="dateVisible1=true">{{dateValue1}}</view>
							<!-- <view class="data-text">2020-12-12</view> -->
							<view class="line"></view>
							<view class="data-text" @tap="timeVisible1=true">{{timeValue1}}</view>
							<!-- <view class="data-text">15:15:15</view> -->	
						</view>
						<text class="text">至</text>
						<view class="selector">
							<view class="data-text" @tap="dateVisible2=true">{{dateValue2}}</view>
							<view class="line"></view>
							<view class="data-text" @tap="timeVisible2=true">{{timeValue2}}</view>
						</view>
					</view>
				</view>
				<view class="search">
					<button class="search-btn" type="default" @tap="onSearch">搜索</button>
				</view>	
				<!-- 前日期 -->
				<w-picker
					:visible.sync="dateVisible1"
					mode="date" 
					:startYear="startYear" 
					:endYear="endYear"
					:value="dateValue1"
					:current="false"
					fields="day"
					@confirm="onBeforeDate($event,'date1')"
					@cancel="onCancel"
					:disabled-after="false"
					ref="date1"
				></w-picker>
				<!-- 前时间 -->
				<w-picker
					:visible.sync="timeVisible1"
					mode="time" 
					:value="timeValue1"
					:current="true"
					:second="true"
					@confirm="onBeforeTime($event,'time1')"
					@cancel="onCancel"
					ref="time1" 
				></w-picker>
				<!-- 后日期 -->
				<w-picker
					:visible.sync="dateVisible2"
					mode="date" 
					:startYear="startYear"
					:endYear="endYear"
					:value="dateValue2"
					:current="true"
					fields="day"
					@confirm="onAfterDate($event,'date2')"
					@cancel="onCancel"
					:disabled-after="false"
					ref="date2"
				></w-picker>
				<!-- 后时间 -->
				<w-picker
					:visible.sync="timeVisible2"
					mode="time" 
					:value="timeValue2"
					:current="true"
					:second="true"
					@confirm="onAfterTime($event,'time2')"
					@cancel="onCancel"
					ref="time2" 
				></w-picker>
			</view> 
		</uni-popup>
	</view>
</template>

<script>
	import { workWithData } from '../../utils/tool.js'
	import { getInventory, inventoryOperation, abnormalStock, getPlasticGoods } from '../../utils/api.js'
	import keyboardListener from '../../components/keyboard-listener/keyboard-listener.vue'
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import MessageContont from '../../components/xiaoxizhongxin/message-contont.vue'
	import InventoryList from '../../components/inventory/inventory-list.vue'
	import ContontNavBar from '../../components/common/contont-nav-bar.vue'
	let str = ''
	export default {
		components: {
			ContontNavBar,
			InventoryList,
			MessageContont,
			uniPopup,
			keyboardListener
		},
		data() {
			return {
				mContont:{},
				department: '',  //科室输入框内容
				personnel: '', //人员输入框内容
				types: '', //耗材种类输入框内容
				dateVisible1:false,
				timeVisible1:false,
				dateVisible2:false,
				timeVisible2:false,
				dateValue1: '',
				timeValue1: '',
				dateValue2: '',
				timeValue2: '',
				startYear: '',
				endYear:'',
				inventoryList: [],
				text: '',
				message: ''
			}
		},
		onLoad() {
			this.loadInvData()
			// this.loadList()
		},
		methods: { 
			onSearchFind(){
				let dataObj = this.forMart()
				console.log(dataObj);
				this.dateValue1 = `${dataObj.year}-${dataObj.oldMonth}-${dataObj.day}`
				// this.dateValue1 = `2021-03-30`
				this.timeValue1 = `${dataObj.hour}:${dataObj.minute}:${dataObj.seconds}`
				this.dateValue2 = `${dataObj.year}-${dataObj.month}-${dataObj.day}`
				this.timeValue2 = `${dataObj.hour}:${dataObj.minute}:${dataObj.seconds}`
				this.startYear = `${dataObj.year}`
				this.endYear = `${dataObj.year}`
				this.department = ''
				this.personnel = ''
				this.types = ''
				this.$refs['popup-datafind'].open()
			},
			onLocation(){
				this.message = ''
				console.log('11');
				this.$refs['popup-location'].open()
			},
			loadInvData(){
				let tokenData = uni.getStorageSync('lastToken');
				getInventory({
					startTime:'', //开始时间 yyyy-MM-dd HH:mm:ss
					endTime:'', //结束时间
					operatorName:'', //操作人登录名
					deptName:'', //科室名称
					itemName:''	 ,//耗材名称
					terminalId:tokenData.terminalId
				}).then(res=>{
					console.log('库存信息',res.data.length)
					this.inventoryList = res.data
					// this.loadList()
				})
			},		
			// async onKeydown(e){
			// 	// console.log(e);
			// 	// console.log(e.key);
			// 	str = str + e.key
			// 	if(str.indexOf("Enter") != -1){
			// 		let res = str.replace(/(Shift|Enter)/gi, "")
			// 		// console.log(str);
			// 		console.log(res); 
			//		let res1 = workWithData(res) //处理扫描枪返回的数据
			// 		const { data } = await getPlasticGoods({barcode: res1}) //根据货物id获取货物信息
			// 		console.log('货物信息',data);
			// 		let isBoolean = this.inventoryList.some(item=>{
			// 			return item.barcode===data.barcode
			// 		})
			// 		const res2 = await inventoryOperation({
			//			operStartTime:'2021-3-12 14:14:14', //用户操作开始的时间
			//			operEndTime:'2021-3-12 14:14:14', //用户操作开始的时间
			//			sendTime:'2021-3-12 14:14:14', //发送时间
			//			datas:[{
			//				barcode: data.barcode, //条码
			//				operType: isBoolean ? '2':'1', //操作类型 1-入库 2-出库
			//				seatId: isBoolean ? '':'1', //货位id，入库时必须，出库时可为空
			//			}]
			//		},true)
			//		console.log('库存操作',res2);
			// 	}
			// },
			// loadList(){
			// 	if(this.inventoryList.length<10){ 
			// 		for(let i=this.inventoryList.length;i<10;i++){
			// 			this.inventoryList[i]={datetime:0}
			// 		} 
			// 	}
			// },
			onBack() {
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			onDetails(index) {   //信息详情
				console.log('onDetails'+index);
				this.mContont = this.inventoryList[index]
				//打开弹出层
				this.$refs.popup.open()
			}, 
			onClose(){ 
				this.mContont = {}
				//关闭弹出层
				this.$refs.popup.close()
			},
			 onfind(text) { //根据耗材名称查询
				console.log('onfind'+ text);
				let tokenData = uni.getStorageSync('lastToken');
				getInventory({
					startTime:'', //开始时间 yyyy-MM-dd HH:mm:ss
					endTime:'', //结束时间
					operatorName:'', //操作人登录名
					deptName:'', //科室名称
					itemName:text  ,//耗材名称
					terminalId:tokenData.terminalId
				}).then(res=>{
					console.log('查询耗材库存信息',data)
					this.inventoryList = data
					// this.loadList()
					this.$refs['popup-location'].close()
				})
			},
			Close() {
				this.$refs['popup-datafind'].close()
			},
			async onSearch() { //查询
				console.log(this.dateValue1,this.timeValue1,this.dateValue2,this.timeValue2,this.personnel,this.department,this.types);
				const { data } = await getInventory({
					startTime:`${this.dateValue1} ${this.timeValue1}`, //开始时间 yyyy-MM-dd HH:mm:ss
					endTime:`${this.dateValue2} ${this.timeValue2}`, //结束时间
					operatorName:this.personnel, //操作人登录名
					deptName:this.department, //科室名称
					itemName:this.types	 //耗材名称
				})
				console.log('查询库存信息',data)
				this.inventoryList = data
				// this.loadList()
				this.Close()
			},
			radioChange(e){
				this.videoType = e.detail.value
			},
			onBeforeDate(res,type){ //前日期
				this.dateValue1= res.result
			},
			onBeforeTime(res,type){ //前时间
				this.timeValue1= res.result
			},
			onAfterDate(res,type){ //后日期
				this.dateValue2= res.result
			},
			onAfterTime(res,type){ //后时间
				this.timeValue2= res.result
			},
			onCancel(){},
			checkClick(index){ //单选按钮点击
				this.currentIndex = index
			},
			forMart(){
				let dates = new Date()
				let year = dates.getFullYear()
				let oldMonth =  dates.getMonth()
				let month = dates.getMonth() + 1
				let day = dates.getDate()
				let hour = dates.getHours() < 10 ? "0" + dates.getHours() : dates.getHours()
				let minute = dates.getMinutes() < 10 ? "0" + dates.getMinutes() : dates.getMinutes()
				let seconds = dates.getSeconds() < 10 ? "0" + dates.getSeconds() : dates.getSeconds()
				month >= 1 && month <= 9 ? (month = "0" + month) : ""
				oldMonth >= 1 && oldMonth <= 9 ? (oldMonth = "0" + oldMonth) : ""
				day >= 0 && day <= 9 ? (day = "0" + day) : ""
				// return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+seconds
				return {	
					'year':year,
					'month':month,
					'day':day,
					'hour':hour,
					'minute':minute,
					'seconds':seconds,
					'oldMonth':oldMonth
				}
			}
		}
	}
</script>

<style scoped lang="less">
.inventory-contont{
	background: #F1F1F1;
	width: 100%;
	height: 100vh;
	// height: calc(100vh - 72px);
}	
.inventory-info{
	// margin: 38rpx 36rpx 32rpx 32rpx;
	margin: 2.4vh 36rpx 3.15vh 32rpx;
	box-sizing: border-box;
	// width: 2802rpx;
	// height: 1304rpx;
	width: 97.5vw;
	height: 85vh;
	border-radius: 32rpx;
	border: 2rpx solid #4F9AFF;
	box-shadow: 0px 2px 5px 0px rgba(3, 28, 62, 0.24);
	position: relative;
	.inventory-bottom{
		position: absolute;
		right: 0;
		left: 0;
		bottom: 0;
		// height: 138rpx;
		height: 9vh;
		background: #4F9AFF;
		border-radius: 0px 0px 10px 10px;
	}	
	
}	
.find-data{
	background: #FFFFFF;
	// width: 886rpx;
	width: 30.76vw;
	height: 100%;
	position: fixed;
	right: 0;
	top: 0;
	.top{
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 51rpx 0 42rpx 0;
		padding-right: 66rpx;
		box-sizing: border-box;
		.top-l{
			background: ;
			display: flex;
			align-items: center;
			justify-content: center;
			// width: 226rpx;
			// height: 87rpx;
			width: 8vw;
			height: 5.63vh;
			background: linear-gradient(180deg, rgba(114, 180, 255, 0.9), rgba(73, 153, 235, 0.9), rgba(43, 125, 209, 0.9));
			box-shadow: 0px 0px 2px 0px rgba(27, 67, 140, 0.64);
			border-radius: 0 87rpx 87rpx 0;
			.top-text{
				font-size: 41rpx;
				color: #FFFFFF;
				letter-spacing: 16rpx;
			}
		}
		.top-r{
			// width: 68rpx;
			// height: 68rpx !important;
			width: 2.37vw;
			height: 4.45vh !important;
		}
	}
	.nav{
		// width: 790rpx;
		width: 27.44vw;
		height: 42.5vh;
		margin: 0 48rpx 26rpx 48rpx;
		border-radius: 20rpx;
		background: #F1F9FF;
		box-sizing: border-box;
		.nav-text{
			font-size: 34rpx;
			color: #777777;
			// margin: 0 0 28rpx 30rpx;
			margin: 0 0 1.83vh 30rpx;
			&:first-child{
				// padding-top: 34rpx;
				padding-top: 2.4vh;
			}
		}
		.input-box{
			display: flex;
			justify-content: center;
			// margin-bottom: 51rpx;
			margin-bottom: 3.26vh;
			position: relative;
			&:last-child{
				// margin-bottom: 34rpx;
				margin-bottom: 2.4vh;
			}
		}
		.nav-input{
			background: #FFFFFF;
			// width: 588rpx;
			// height: 80rpx;
			width: 20.42vw;
			height: 5.23vh;
			border: 4rpx solid #4F9AFF;
			border-radius: 75rpx;
			font-size: 34rpx;
			color: #000;
			padding: 0 80rpx 0 50rpx;	
			box-sizing: border-box;
		}
		.drop-down{
			width: 35rpx;
			height: 17rpx;
			position: absolute;
			top: 50%;
			right: 140rpx;
			transform: translateY(-50%);
		}
	}
	.time-list{
		box-sizing: border-box;
		// width: 790rpx;
		width: 27.44vw;
		// height: 345rpx;
		height: 24.5vh;
		margin: 0 48rpx 120rpx 48rpx;
		padding: 32rpx 34rpx;
		border-radius: 20rpx;
		background: #F1F9FF;
		.time-text{
			font-size: 34rpx;
			color: #777777;
		}
		.time-selector{
			padding: 34rpx 0 0 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			box-sizing: border-box;
			.selector{
				box-sizing: border-box;
				// width: 292rpx;
				// height: 210rpx;
				width: 11.14vw;
				height: 13.73vh;
				border: 4rpx solid #4F9AFF;
				background: #FFFFFF;
				border-radius: 24rpx;
				padding: 0 34rpx;
				text-align: center;
				.data-text{
					font-size: 35rpx;
					color: #777777;
					// margin: 27rpx 0 27rpx 0;
					margin: 1.7vh 0 1.7vh 0;
					font-weight: 400;
				}
				.line{
					width: 100%;
					height: 4rpx;
					background: #4F9AFF;
				}
			}
			.text{
				font-size: 34rpx;
				color: #777777;
			}
		}
	}
	.search{
		display: flex;
		align-items: center;
		justify-content: center;
		.search-btn{
			display: flex;
			align-items: center;
			justify-content: center;
			// width: 478rpx;
			// height: 110rpx;
			width: 16.67vw;
			height: 7.19vh;
			background: #4F9AFF;
			color: #FFFFFF;
			font-size: 41rpx;
			border-radius: 110rpx;
			letter-spacing: 5rpx;
		}
	}
}
.location-find{
	width: 872rpx;
	height: 620rpx;
	background: #FFFFFF;
	border-radius: 24rpx;
	.layber-on{
		width: 100%;
		height: 120rpx;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		justify-content: space-between;
		padding: 0 48rpx;
		background: #4F9AFF;
		border-radius: 24rpx 24rpx 0px 0px;
		.nav-title{
			color: #FFFFFF;
			font-size: 48rpx;
			letter-spacing: 5rpx;
		}
		.nav-guanbi{
			width: 80rpx;
			height: 80rpx !important;
		}
	}
	.layber-in{
		box-sizing: border-box;
		padding: 56rpx 52rpx;
		.layber-name{	
			color: #777777;
			font-size: 40rpx;
			margin-bottom: 40rpx;
		}
		.layber-info{
			display: flex;
			justify-content: center;
			flex-direction: column;
			align-items: center;
		}
		.layber-input{
			width: 588rpx;
			height: 104rpx;
			border: 4rpx solid #4F9AFF;
			border-radius: 104rpx;
			box-sizing: border-box;
			margin-bottom: 82rpx;
			padding:0 40rpx;
			font-size: 40rpx;
			color: #717171;
		}
		.layber-btn{
			width: 346rpx;
			height: 100rpx;
			background: #4F9AFF;
			color: #FFFFFF;
			border-radius: 100rpx;
			font-size: 44rpx;
			letter-spacing: 8rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			box-sizing: border-box;
		}
	}
}
.inventory{
	position: fixed;
	top: 50%;
	left: 50%;
	transform: translateX(-50%) translateY(-50%);
	width: 390rpx;
	height: 390rpx;
	background: transparent;
	display: flex;
	align-items: center;
	flex-direction: column;
	justify-content: center;
	.inventory-img{
		width: 300rpx;
		height: 248rpx;
		background: url(../../static/1.gif) no-repeat;
		background-size: cover;
		margin-bottom: 40rpx;
	}
	.inventory-text{
		font-size: 40rpx;
		color: #2180EA;
	}
}

.contont{
	height: 100%;
	box-sizing: border-box;
	width: 100%;
	padding: 0 60rpx;
	.v1{
		width: 100%;
		height: 100%;
		display: flex;
		align-items: center;
		flex: 1;
		justify-content: space-between;
	}
	.chaxun-img{
		width: 250rpx;
		height: 88rpx;
		margin-right: 32rpx;
	}
	.bottom-l{
		width: 33.3%;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: flex-start;
		.jiankong1-img{
			width: 306rpx;
			height: 88rpx;
			margin-right: 32rpx;
		}
		.jiankong2-img{
			width: 306rpx;
			height: 88rpx;
		}
	}
	.bottom-c{
		width: 33.3%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 52rpx;
		color: #FFFFFF;
		letter-spacing: 5rpx;
	}
	.bottom-r{
		display: flex;
		align-items: center;
		justify-content: flex-end;
		width: 33.3%;
		height: 100%;
		// .jiangkong3-img{
		// 	width: 306rpx;
		// 	height: 88rpx;
		// 	margin-right: 32rpx;
		// }
	}
	.back-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 250rpx;
		height: 88rpx;
		background: #FFFFFF;
		border-radius: 88rpx;
		.back-img{
			width: 48rpx;
			height: 40rpx;
			margin-right: 16rpx;
		}
		.back-text{
			font-size: 40rpx;
			color: #4F9AFF;
		}
	}
}

.popup-push{
	// width: 2400rpx;
	// height: 1316rpx;
	width: 83.3vw;
	height: 86vh;
	background: #FFFFFF;
	border-radius: 20rpx;
	position: relative;
	.nav-title{
		width: 100%;
		// height: 132rpx;
		height: 8.63vh;
		border-radius: 20rpx 20rpx 0 0;
		background: #4F9AFF;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 48rpx;
		color: #FFFFFF;
	}
	.nav-contont{
		width: 100%;
		padding: 48rpx 56rpx 0 56rpx;
		box-sizing: border-box;
		.content-text{
			font-size: 40rpx;
			text-indent: 2em;
			color: #515151;
		}
		.content-end{
			display: flex;
			justify-content: flex-end;
			margin-top: 44rpx;
			font-size: 40rpx;
			color: #777777;
		}
		.content-date{
			display: flex;
			justify-content: flex-end;
			margin-top: 44rpx;
			font-size: 36rpx;
			color: #777777;
		}
	}
	.nav-bottom{
		position: absolute;
		left: 0;
		bottom: 0;
		width: 100%;
		// height: 200rpx;
		height: 13.1vh;
		padding: 0 88rpx;
		display: flex;
		align-items: center;
		flex: 1;
		justify-content: space-between;
		border-top: 2rpx solid #EEEEEE;
		box-sizing: border-box;
		.left-tupiao{
			display: flex;
			align-items: center;
			justify-content: flex-start;
			width: 33%;
			height: 100%;
			.tupiao{
				width: 44rpx;
				height: 44rpx;
				margin-right: 16rpx;
			}
			.text{
				font-size: 44rpx;
				color: #FF4F4F;
			}
		}
		.center-btn{
			width: 33%;
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			.btn{
				display: flex;
				align-items: center;
				justify-content: center;
				width: 346rpx;
				height: 96rpx;
				font-size: 48rpx;
				color: #FFFFFF;
				background: #4F9AFF;
				border-radius: 96rpx;
			}
		}
		.kong{
			width: 33%;
			height: 100%;
		}
	}
}
</style>
